<!DOCTYPE html>
<html>
    
    <head>
        <meta charset="utf-8">
        <title>
            编辑
        </title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="format-detection" content="telephone=no">
        <link rel="stylesheet" href="../../css/x-admin.css" media="all">
        <script type="text/javascript" src="../../js/jquery.min.js"></script>
        <script type="text/javascript" src="../../js/frame.js"></script>
    </head>
    
    <body>
        <div class="x-body">
            <form class="layui-form">
                <div class="layui-form-item">
                    <input type="hidden" id="id" name="id"/>
                    <label for="username" class="layui-form-label">
                        <span class="x-red">*</span>登录名
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="username" name="username" required="" lay-verify="required"
                               autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux">
                        <span class="x-red">*</span>将会成为您唯一的登入名
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="phone" class="layui-form-label">
                        <span class="x-red">*</span>手机
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="phone" name="phone" required="" lay-verify="phone"
                               autocomplete="off" class="layui-input">
                    </div>
                    <!--<div class="layui-form-mid layui-word-aux">-->
                    <!--<span class="x-red">*</span>-->
                    <!--</div>-->
                </div>
                <div class="layui-form-item">
                    <label for="email" class="layui-form-label">
                        <span class="x-red">*</span>邮箱
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="email" name="email" required="" lay-verify="email"
                               autocomplete="off" class="layui-input">
                    </div>
                    <!--<div class="layui-form-mid layui-word-aux">-->
                    <!--<span class="x-red">*</span>-->
                    <!--</div>-->
                </div>
                <div class="layui-form-item">
                    <label for="realname" class="layui-form-label">
                        <span class="x-red">*</span>真实姓名
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="realname" name="realname" required="" lay-verify="required"
                               autocomplete="off" class="layui-input">
                    </div>
                    <!--<div class="layui-form-mid layui-word-aux">-->
                    <!--<span class="x-red">*</span>-->
                    <!--</div>-->
                </div>
                <div class="layui-form-item">
                    <label for="sex" class="layui-form-label">
                        <span class="x-red">*</span>性别
                    </label>
                    <div class="layui-input-inline">
                        <select name="sex">
                            <!--<option value="">请选择角色</option>-->
                            <option value="0">女</option>
                            <option value="1">男</option>
                        </select>
                    </div>
                </div>
                <!--<div class="layui-form-item">-->
                <!--<label for="role" class="layui-form-label">-->
                <!--<span class="x-red">*</span>角色-->
                <!--</label>-->
                <!--<div class="layui-input-inline">-->
                <!--<select name="role">-->
                <!--&lt;!&ndash;<option value="">请选择角色</option>&ndash;&gt;-->
                <!--<option value="1">超级管理员</option>-->
                <!--<option value="2">管理员</option>-->
                <!--<option value="3">普通用户</option>-->
                <!--</select>-->
                <!--</div>-->
                <!--</div>-->
                <!--<div class="layui-form-item">-->
                    <!--<label for="pass" class="layui-form-label">-->
                        <!--<span class="x-red"></span>密码-->
                    <!--</label>-->
                    <!--<div class="layui-input-inline">-->
                        <!--<input type="password" id="pass" name="pass" required="" lay-verify="pass"-->
                               <!--autocomplete="off" class="layui-input">-->
                    <!--</div>-->
                    <!--<div class="layui-form-mid layui-word-aux">-->
                        <!--6到16个字符-->
                    <!--</div>-->
                <!--</div>-->
                <!--<div class="layui-form-item">-->
                    <!--<label for="repass" class="layui-form-label">-->
                        <!--<span class="x-red"></span>确认密码-->
                    <!--</label>-->
                    <!--<div class="layui-input-inline">-->
                        <!--<input type="password" id="repass" name="repass" required="" lay-verify="repass"-->
                               <!--autocomplete="off" class="layui-input">-->
                    <!--</div>-->
                <!--</div>-->
                <div class="layui-form-item">
                    <label for="repass" class="layui-form-label">
                    </label>
                    <button  class="layui-btn" lay-filter="save" lay-submit="">
                        保存
                    </button>
                </div>
            </form>
        </div>
        <script src="../../lib/layui/layui.js" charset="utf-8"></script>
        <script src="../../js/x-layui.js" charset="utf-8"></script>
        <script>
            $(function(){
                // alert(getParam("id"));
                $("#id").val(getParam("id"));
                //获取当前登录用户信息
                $.ajax({
                    url:getContextPath()+"/html/getCurrentUser",
                    type:"post",
                    data:"",
                    success:function(data){
                        if(data.success!=false){
                            var user = data.content;
                            if(user==null){
                                window.location.href=getRootPath();
                            }
                        }
                    },
                    error:function(e){
                        alert("系统错误，请联系管理员！！");
                    }
                });
                //查询用户信息
                $.ajax({
                    url:getContextPath()+"/html/findUserById",
                    type:"post",
                    data:{id:getParam("id")},
                    success:function(data){
                        if(data.success!=false){
                            var user = data.content;
                            if(user!=null){
                                $("#username").val(user.userName);
                                // $("#pass").val(user.passWord);
                                $("#realname").val(user.realName);
                                $("#phone").val(user.phone);
                                $("#email").val(user.email);
                                $("#sex").val(user.sex);
                            }
                        }
                    },
                    error:function(e){
                        alert("系统错误，请联系管理员！！");
                    }
                });

            });

            layui.use(['form','layer'], function(){
                $ = layui.jquery;
              var form = layui.form()
              ,layer = layui.layer;
            
              //自定义验证规则
              form.verify({
                nikename: function(value){
                  if(value.length < 5){
                    return '昵称至少得5个字符啊';
                  }
                }
                ,pass: function(value){
                      if($('#pass').val()!=undefined&&$('#pass').val()!=""){
                          // [/(.+){6,12}$/, '密码必须6到12位'];
                          var reg = /(.+){6,12}$/;
                          if(!reg.test($('#pass').val()))
                          return '密码必须6到12位';
                      }
                  }
                ,repass: function(value){
                    if($('#pass').val()!=$('#repass').val()){
                        return '两次密码不一致';
                    }
                }
              });

              //监听提交
              form.on('submit(save)', function(data){
                console.log(data);
                //发异步
                  $.ajax({
                      url: getContextPath() + "/html/updateUserById",
                      type: "post",
                      data: JSON.parse(JSON.stringify(data.field)),
                      success: function (data) {
                          if (data.success != false) {
                              layer.alert("保存成功", {icon: 6},function () {
                                  // 获得frame索引
                                  var index = parent.layer.getFrameIndex(window.name);
                                  //关闭当前frame
                                  parent.layer.close(index);
                                  window.parent.location.reload(); //刷新父页面
                              });
                          }else{
                              layer.alert(data.errmsg, {icon: 6},function () {
                                  // 获得frame索引
                                  layer.closeAll("dialog");
                                  // var index = parent.layer.getFrameIndex(window.name);
                                  //关闭当前frame
                                  // parent.layer.close(index);
                              });
                          }
                      }
                  });
                return false;
              });
              
              
            });
        </script>
    </body>

</html>